package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.IArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.pojos.WmNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 监听文章上下架的类
 */
@Component
public class DownOrUpListener {

    @Autowired
    private IArticleConfigService articleConfigService;

    @KafkaListener(topics = "downOrUp_topic")
    public void getMessage(String message){
        //1.转换格式
        WmNews wmNews = JSON.parseObject(message, WmNews.class);

        Long articleId = wmNews.getArticleId();
        Short enable = wmNews.getEnable();//0表示下架，1表示上架
        //2.同步article-config表的is_down状态值
        articleConfigService.update(
                Wrappers.<ApArticleConfig>lambdaUpdate().
                        set(ApArticleConfig::getIsDown,enable==0?true:false)
                .eq(ApArticleConfig::getArticleId,articleId)
        );

    }
}
